<cfparam name="resetFilters" default="false">
<cfset isFiltered = false />
<cfset queryParams = StructNew() />

<cfset client.orders.Source = "CCFC" />
<cfset queryParams.Source = "CCFC" />

<cfset client.orders.Deleted = 0 />
<cfset queryParams.Deleted = 0 />

<cfset client.orders.fromOrderDate = CreateDate(ListFirst(request.currentProgramYear, "-"), 9, 1) />
<cfset queryParams.fromOrderDate = CreateDate(ListFirst(request.currentProgramYear, "-"), 9, 1) />

<cfset client.orders.toOrderDate = CreateDate(ListLast(request.currentProgramYear, "-"), 8, 30) />
<cfset queryParams.toOrderDate = CreateDate(ListLast(request.currentProgramYear, "-"), 8, 30) />

<!--- statuses --->
<cfif NOT IsDefined("client.orders.statuses") or resetFilters>
	<cfset client.orders.statuses = "All">
<cfelseif IsDefined("filterStatuses")>
	<cfset client.orders.statuses = filterStatuses>
</cfif>
<cfif client.orders.statuses neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.statuses = client.orders.statuses />
</cfif>

<!--- organizationIDs --->
<cfif NOT IsDefined("client.orders.organizationIDs") or resetFilters>
	<cfset client.orders.organizationIDs = "All">
<cfelseif IsDefined("filterOrganizationIDs")>
	<cfset client.orders.organizationIDs = filterOrganizationIDs />
</cfif>
<cfif client.orders.organizationIDs neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.organizationIDs = client.orders.organizationIDs />
</cfif>
<!--- regionIDs --->
<cfif IsUserInRole("Super User")>
	<cfif NOT IsDefined("client.orders.regionIDs") or resetFilters>
		<cfset client.orders.regionIDs = "All">
	<cfelseif IsDefined("filterRegionIDs")>
		<cfset client.orders.regionIDs = filterRegionIDs />
	</cfif>
<cfelseif IsUserInRole("Regional Admin")>
	<cfset client.orders.regionIDs = request.authUser.regionID />
</cfif>
<cfif client.orders.regionIDs neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.regionIDs = client.orders.regionIDs />
</cfif>

<cfwddx action="cfml2wddx" input="#queryParams#" output="client.orders.queryParamsWDDX" />

<cfscript>
	orderGateway = new assets.calgrants.com.gateway.CSACOrderGateway();
	orders = orderGateway.query(argumentCollection = queryParams);
	organizationGateway = new assets.calgrants.com.gateway.OrganizationGateway();
	organizations = organizationGateway.query(regionIDs = IIf(IsUserInRole("Regional Admin"), request.authUser.regionID, ""), Organization_ID = IIf(IsUserInRole("Organizer") OR IsUserInRole("Site Contact"), request.authUser.organizationID, ""));
	storedProcedureGateway = new assets.calgrants.com.gateway.StoredProcedureGateway();
	regions = storedProcedureGateway.call("getDistinctRegions");
	csacBase = new assets.csac.com.Base();
	orderStatuses = csacBase.getOrderStatuses();
</cfscript>

<cfoutput>

	<h3>Orders<cfif isFiltered> <small><em>filtered</em></small></cfif></h3>
	
	<div class="row-fluid" style="margin-bottom: 10px;">
		<div class="span12">
			<a href="#script_name#?view=editCSACOrder&CSAC_Order_ID=0" class="btn">New&nbsp;order</a>
			<a id="filterLink" href="##" class="btn" onclick="toggleFilter('filter')">Show filters</a>
			<!--- <div class="btn-group">
				<button class="btn dropdown-toggle" data-toggle="dropdown">Actions</button>
				<button class="btn dropdown-toggle" data-toggle="dropdown"><span class="caret"></span></button>
				<ul class="dropdown-menu">
					<cfif orders.RecordCount gt 0>
						<li><a href="?action=export&collection=orders" target="_new">Export</a></li>
					<cfelseif NOT IsUserInRole("Super User")>
						<li><a href="##">None - no records available with filter settings</a></li>
					</cfif>
					<cfif IsUserInRole("Super User")>
						<li><a href="?view=ordersSummary&Program_Year=#request.currentProgramYear#&printable=true" target="_new">#request.currentProgramYear# Summary Report</a></li>
					</cfif>
				</ul>
			</div> --->
			<span class="pull-right"><strong>Count:</strong> #orders.RecordCount#</span>
		</div>
	</div>

	<cfform name="ordersForm" id="ordersForm" action="#script_name#" method="post">
		<cfinput type="hidden" name="view" value="#view#">
		
		<div id="filter" class="alert alert-info alert-block" style="display: none;">
			<h4>Filters</h4>
			<table cellpadding="6">
				<tr valign="top">
					<td style="width: 150px;">
						<label>Program Year</label>
						<p>#request.currentProgramYear#</p>
						<!--- <cfselect name="filterProgramYear" size="1" multiple="false" required="yes" message="Program Year filter is required" style="width: 150px;">
							<cfloop index="y" from="2008" to="#ListFirst(application.currentProgramYear, '-')#">
								<cfset py = "#y#-" & y+1>
								<option value="#py#"<cfif client.orders.Program_Year eq py> selected</cfif>>#py#</option>
							</cfloop>
						</cfselect> --->
					</td>
					<cfif IsUserInRole("Super User")>
						<td>
							<label>Regions</label>
							<cfselect name="filterRegionIDs" size="7" multiple="true" required="yes" message="Region filter is required" style="width: 150px;">
								<option value="All"<cfif client.orders.regionIDs eq "All"> selected</cfif>>All</option>
								<cfloop query="regions">
								<option value="#Region_ID#"<cfif ListFind(client.orders.regionIDs, Region_ID)> selected</cfif>>#Region_ID#</option>
								</cfloop>
							</cfselect>
						</td>
					</cfif>
					<td>
						<label>Organizations</label>
						<cfselect name="filterOrganizationIDs" size="7" multiple="true" required="yes" message="Organization filter is required" style="width: auto;">
							<option value="All"<cfif client.orders.organizationIDs eq "All"> selected</cfif>>All</option>
							<cfloop query="organizations">
							<option value="#Organization_ID#"<cfif ListFind(client.orders.organizationIDs, Organization_ID)> selected</cfif>>#Organization# - #City#</option>
							</cfloop>
						</cfselect>
					</td>
					<td>
						<label>Status</label>
						<cfselect name="filterStatuses" size="6" multiple="true" required="yes" message="Status filter is required" style="width: auto;">
							<option value="All"<cfif client.orders.statuses eq "All"> selected</cfif>>All</option>
							<cfloop index="i" from="1" to="#ArrayLen(orderStatuses)#">
							<option value="#orderStatuses[i].value#"<cfif ListFind(client.orders.statuses, orderStatuses[i].value) gt 0> selected</cfif>>#orderStatuses[i].text#</option>
							</cfloop>
						</cfselect>
					</td>
				</tr>
				<tr>
					<td colspan="5">
						<button type="submit" class="btn btn-info">Apply</button> <a href="#script_name#?view=orders&resetFilters=true" class="btn btn-info">Clear</a>
					</td>
				</tr>
			</table>
		</div>

		<cfif orders.RecordCount gt 0>

			<table id="ordersList" style="display: none; width: 100%">
				<thead>
					<th>ID</th>
					<th>Program Year</th>
					<th>Region</th>
					<th>Order Date</th>
					<th>First Workshop Date</th>
					<th>Approved Date</th>
					<th>Shipped Date</th>
					<th>Organization</th>
					<th>Ordered By</th>
					<th>Status</th>
				
				<tbody>
					<cfloop query="orders">   
						<tr>
							<td>
								<cfif Order_Date gte CreateDate(2013, 9, 1)>
									<cfif IsUserInRole("Super User") and Status neq "Staged" and Status neq "Shipped">
										<a href="#script_name#?view=editCSACOrder&returnView=#view#&CSAC_Order_ID=#CSAC_Order_ID#">#Order_ID#</a>
									<cfelse>
										<a href="#script_name#?view=csacOrder&returnView=#view#&CSAC_Order_ID=#CSAC_Order_ID#">#Order_ID#</a>
									</cfif>
								<cfelse>
									#Order_ID#
								</cfif>					
							</td>
							<td>#Program_Year#</td>
							<td>#County_Region_ID#</td>
							<td><span class="sortHide">#DateFormat(Order_Date, 'yyyymmdd')#</span>#DateFormat(Order_Date, "m/d/yyyy")#</td>
							<td><span class="sortHide">#DateFormat(Next_Workshop_Date, 'yyyymmdd')#</span>#DateFormat(Next_Workshop_Date, "m/d/yyyy")#</td>
							<td>
								<cfif IsDate(Approved_Date)><span class="sortHide">#DateFormat(Approved_Date, 'yyyymmdd')#</span>#DateFormat(Approved_Date, "m/d/yyyy")#</cfif>
							</td>
							<td>
								<cfif IsDate(Shipped_Date)><span class="sortHide">#DateFormat(Shipped_Date, 'yyyymmdd')#</span>#DateFormat(Shipped_Date, "m/d/yyyy")#</cfif>
							</td>
							<td><cfif Organization_Deleted>
									<font color="red">#Organization_Organization#</font>
								<cfelseif Organization_Archived>
									<font color="silver">#Organization_Organization#</font>
								<cfelseif NOT Organization_Approved>
									<font color="orange">#Organization_Organization#</font>
								<cfelseif Organization_Approved>
									#Organization_Organization#
								</cfif>
							</td>
							<td>
								<cfif Ordered_By_Deleted>
									<font color="red">#Ordered_By_First_Name# #Ordered_By_Last_Name#</font>
								<cfelseif Ordered_By_Archived>
									<font color="silver">#Ordered_By_First_Name# #Ordered_By_Last_Name#</font>
								<cfelseif NOT Ordered_By_Approved>
									<font color="orange">#Ordered_By_First_Name# #Ordered_By_Last_Name#</font>
								<cfelseif Ordered_By_Approved>
									#Ordered_By_First_Name# #Ordered_By_Last_Name#
								</cfif>
							</td>
							<td>#Status#</td>
						</tr>
					</cfloop>
				</tbody>
			</table>
			
			<script type="text/javascript">
				$(function() {
					$("##ordersList").kendoGrid({
						sortable: {
							mode: "single",
							allowUnsort: false
						}			
					})
					$("##ordersList").show();
					var ordersListGrid = $("##ordersList").data("kendoGrid");
					ordersListGrid.bind("dataBound", function(e) {
						//console.log(e);
					});
				})
			</script>
			
		<cfelse>
		
			<div class="alert alert-info notFound">No orders</div>
	
			<script type="text/javascript" language="javascript">
			<!--
				$(function() {
					$("##filter").show();
				});
			// -->
			</script>
			
		</cfif>

	</cfform>

</cfoutput>